import axios from "axios";
import store from "@/store";
import { Message } from "element-ui";
import router from "@/router";
const service = axios.create({
  baseURL: 'api',//基础地址
  timeout: 10000,
});//创建一个新的axios实例

service.interceptors.request.use((config) => { 
  //注入token
  // this.$store.getters
  // store.getters.token =>请求头里
  if (store.getters.token) {
     config.headers.Authorization = `Bearer ${store.getters.token}`;
  }
  return config
}, (error) => {
  //失败执行promise
  return Promise.reject(error);
})
// 响应拦截器
service.interceptors.response.use((response) => {
  const { data, message, success } = response.data // 默认json格式
  if (success) {
    return data
  } else {
    Message({ type: 'error', message })
    return Promise.reject(new Error(message))
  }
}, async (error) => {
  if (error.response.status === 401) {
    Message({ type: 'error', message: 'token超时了' })
    //说明token超时了
   await store.dispatch('user/logout')//调用action退出登录
    //主动跳到登录页
    router.push('/login')//跳转到登录页
    return Promise.reject(error)

  }
  // error.message
  Message({ type: 'error', message: error.message })
  return Promise.reject(error)
})

export default service;